<!DOCTYPE html>
<html lang="en">
<head>
        <title>Debugging Linux start-up on Altera Cyclone V SoC with OpenOCD</title>
        <meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="shortcut icon" href="./theme/images/favicon.ico"/>
        <link rel="stylesheet" href="./theme/css/main.css" type="text/css" />
        <link href="www.j-marjanovic.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="j-marjanovic.io Atom Feed" />

        <!--[if IE]>
                <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->

        <!--[if lte IE 7]>
                <link rel="stylesheet" type="text/css" media="all" href="./css/ie.css"/>
                <script src="./js/IE8.js" type="text/javascript"></script><![endif]-->

        <!--[if lt IE 7]>
                <link rel="stylesheet" type="text/css" media="all" href="./css/ie6.css"/><![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="theme/jquery.cookiesdirective.js"></script>

</head>

<body id="index" class="home">
<script type="text/javascript">
	// Using $(document).ready never hurts
	$(document).ready(function(){

		// Cookie setting script wrapper
		var cookieScripts = function () {
			// Internal javascript called
			console.log("Running");
		
			// Loading external javascript file
			$.cookiesDirective.loadScript({
				uri:'external.js',
				appendTo: 'eantics'
			});
		}
	
		/* Call cookiesDirective, overriding any default params
		
			*** These are the defaults ***
				explicitConsent: true,
				position: 'top',
				duration: 10,
				limit: 0,
				message: null,				
				cookieScripts: null,
				privacyPolicyUri: 'privacy.html',
				scriptWrapper: function(){},	
				fontFamily: 'helvetica',
				fontColor: '#FFFFFF',
				fontSize: '13px',
				backgroundColor: '#000000',
				backgroundOpacity: '80',
				linkColor: '#CA0000'
				
		*/
		
		$.cookiesDirective({
			privacyPolicyUri: 'myprivacypolicy.html',
			explicitConsent: false,
			position : 'bottom',
			scriptWrapper: cookieScripts, 
			cookieScripts: 'Google Analytics', 
			backgroundColor: '#52B54A',
			linkColor: '#ffffff'
		});
	});
</script>

	
  <!-- <header id="banner" class="body"> -->
  <!--               <h1><a href="./"><img src="http://www.launchyard.com/images/logo.png" /></a></h1> -->
  <!--       </header> --> 

  <div class="LaunchyardDetail" style="align:right;">
    <!-- <p> -->
    <!-- <img src="./theme/images/blue-pin.png" width="100" height="100" alt="Graph icon"> -->
    <!-- </p> -->
    <p><a id="sitesubtitle" href="./">j-marjanovic.io</a></p>

	<br>
    <p style="float: right; margin-right: 50px;"><a id="aboutlink" href="./pages/about.html">About</a></p>

    <br>
	<p style="float: right; margin-right: 50px;"><img src="./theme/images/icons/rss.png"> <a id="aboutlink" href="./feeds/jan-marjanovic.atom.xml">Atom feed</a></p>
    <br>

  </div>

<section id="content" >
    <div class="body">
      <article>
        <header>
          <h1 class="entry-title">
            <a href="./debugging-linux-start-up-on-altera-cyclone-v-soc-with-openocd.html" rel="bookmark"
               title="Permalink to Debugging Linux start-up on Altera Cyclone V SoC with OpenOCD">Debugging Linux start-up on Altera Cyclone V SoC with OpenOCD</a></h1>

        </header>

        <div class="entry-content">
<div class="post-info">
	<ul>
        <li class="vcard author">
                 by&nbsp;<a class="url fn" href="./author/jan-marjanovic.html">Jan Marjanovic</a>
        </li>
        <li class="published" title="2016-07-12T22:00:00+02:00">
          on&nbsp;Tue 12 July 2016
        </li>

	</ul>
<p>Category: <a href="./tag/fpga.html">FPGA</a>, <a href="./tag/altera.html">Altera</a>, <a href="./tag/linux.html">Linux</a>, <a href="./tag/openocd.html">OpenOCD</a>, </p>
</div><!-- /.post-info -->          <p>This blog post will show you how one can use the OpenOCD debugger with Altera
Cyclone V SoC. Altera Cyclone V SoC is a very interesting integrated circuit,
combining dual-core ARM processor and a decent FPGA, allowing a wide variety
of possibilities to partition the application between the two.</p>
<p>Xilinx offers Xilinx SDK as the tool to program and debug their MicroBlaze
soft-core and ARM cores in their Zynq FPGAs. Altera on the other hand has two
different tools to program and debug their portfolio of processors. There is
Nios II EDS which provides support for Nios soft-core processor and there is 
ARM DS-5 Development Studio, which provides support for ARM cores in Altera SoCs. While I believe
DS-5 can be useful tool, unfortunately the free-as-a-beer <a href="https://developer.arm.com/products/software-development-tools/ds-5-development-studio/editions/customized-editions/altera/community-edition">DS-5 Community Edition</a>
only allows debugging Linux user-space applications. In order to use it, the 
Linux should be up and running in order to run gdbserver on processor.</p>
<p><img alt="Debug options in DS-5 Altera Community Edition" src="./images/debugging_cyclone_soc_openocd/ds5_debug_options.png" style="max-width:100%; width: auto; display: block; margin-left: auto; margin-right: auto;"></p>
<p>When doing initial bring-up or experimenting this may not be the case. If
there is something wrong with a kernel, a device tree or the drivers, one can
easily find himself with a non-responsive system. Even in this case the JTAG
debugger offers a side-door access to the system. This greatly simplifies
determining the cause which lead to the system halt.</p>
<h1>Installation of OpenOCD</h1>
<h2>OpenOCD</h2>
<p><a href="http://openocd.org/">OpenOCD</a> is a free and open-source on-chip debugger. It
provides a link between hardware components and a command line interface,
which can be used to control and monitor the hardware over JTAG interface. It
can also be interfaced with GDB (GNU Debugger) integrated with Eclipse, to
provide a graphical way to debug programs. If you want to know more, at the
bottom of <a href="http://openocd.org/documentation/">OpenOCD Documentation page</a> is
a link to the presentation on FOSDEM 2006.</p>
<p>First we need to install all needed tools:</p>
<div class="highlight"><pre><span></span><code><span class="err">sudo apt-get install libtool autotools-dev automake libusb-1.0 libhidapi-dev pkg-config git</span>
</code></pre></div>


<p>Then we get the source code from OpenOCD SourceForge repository. I have used the
latest available commit in master, which was the one with a git tree-ish value
of <code>12ff09f</code>:</p>
<div class="highlight"><pre><span></span><code><span class="n">git</span><span class="w"> </span><span class="n">clone</span><span class="w"> </span><span class="nl">git</span><span class="p">:</span><span class="o">//</span><span class="n">git</span><span class="p">.</span><span class="n">code</span><span class="p">.</span><span class="n">sf</span><span class="p">.</span><span class="n">net</span><span class="o">/</span><span class="n">p</span><span class="o">/</span><span class="n">openocd</span><span class="o">/</span><span class="n">code</span><span class="w"> </span><span class="n">openocd</span><span class="o">-</span><span class="n">code</span><span class="w"></span>

<span class="n">root</span><span class="nv">@eee2f562f8f2</span><span class="err">:</span><span class="o">/</span><span class="n">openocd</span><span class="o">-</span><span class="n">code2</span><span class="err">#</span><span class="w"> </span><span class="n">git</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="n">HEAD</span><span class="w"></span>
<span class="k">commit</span><span class="w"> </span><span class="mi">12</span><span class="n">ff09f7f27a707fe42226262f55b8ce8351cbf9</span><span class="w"></span>
<span class="nl">Author</span><span class="p">:</span><span class="w"> </span><span class="n">Esben</span><span class="w"> </span><span class="n">Haabendal</span><span class="w"> </span><span class="o">&lt;</span><span class="n">esben</span><span class="nv">@haabendal</span><span class="p">.</span><span class="n">dk</span><span class="o">&gt;</span><span class="w"></span>
<span class="nc">Date</span><span class="err">:</span><span class="w">   </span><span class="n">Fri</span><span class="w"> </span><span class="n">Nov</span><span class="w"> </span><span class="mi">27</span><span class="w"> </span><span class="mi">09</span><span class="err">:</span><span class="mi">13</span><span class="err">:</span><span class="mi">36</span><span class="w"> </span><span class="mi">2015</span><span class="w"> </span><span class="o">+</span><span class="mi">0100</span><span class="w"></span>
<span class="nl">cfi</span><span class="p">:</span><span class="w"> </span><span class="k">Add</span><span class="w"> </span><span class="n">support</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">strangely</span><span class="w"> </span><span class="n">endianness</span><span class="w"> </span><span class="n">broken</span><span class="w"> </span><span class="n">SoC</span><span class="w"> </span><span class="n">implementations</span><span class="w"></span>
</code></pre></div>


<p>Perform all needed steps to compile the code (have a look in <code>INSTALL</code> for
detailed instructions):</p>
<div class="highlight"><pre><span></span><code><span class="err">cd openocd-code</span>
<span class="err">aclocal</span>
<span class="err">./bootstrap</span>
</code></pre></div>


<p>At the end of configure step make sure that support for Altera USB-Blaster II
and CMSIS-DAP Debugger are configured. The output should look something like
this:</p>
<div class="highlight"><pre><span></span><code><span class="p">.</span><span class="o">/</span><span class="n">configure</span>

<span class="p">...</span>

<span class="n">OpenOCD</span> <span class="n">configuration</span> <span class="n">summary</span>
<span class="c1">--------------------------------------------------</span>
<span class="n">MPSSE</span> <span class="k">mode</span> <span class="k">of</span> <span class="n">FTDI</span> <span class="n">based</span> <span class="n">devices</span>        <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
<span class="n">Segger</span> <span class="n">J</span><span class="o">-</span><span class="n">Link</span> <span class="n">JTAG</span> <span class="n">Programmer</span>           <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
<span class="n">ST</span><span class="o">-</span><span class="n">Link</span> <span class="n">JTAG</span> <span class="n">Programmer</span>                 <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
<span class="n">TI</span> <span class="n">ICDI</span> <span class="n">JTAG</span> <span class="n">Programmer</span>                 <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
<span class="n">Keil</span> <span class="n">ULINK</span> <span class="n">JTAG</span> <span class="n">Programmer</span>              <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
<span class="n">Altera</span> <span class="n">USB</span><span class="o">-</span><span class="n">Blaster</span> <span class="n">II</span> <span class="n">Compatible</span>        <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
<span class="n">Versaloon</span><span class="o">-</span><span class="n">Link</span> <span class="n">JTAG</span> <span class="n">Programmer</span>          <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
<span class="n">OSBDM</span> <span class="p">(</span><span class="n">JTAG</span> <span class="k">only</span><span class="p">)</span> <span class="n">Programmer</span>            <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
<span class="n">eStick</span><span class="o">/</span><span class="n">opendous</span> <span class="n">JTAG</span> <span class="n">Programmer</span>         <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
<span class="n">Andes</span> <span class="n">JTAG</span> <span class="n">Programmer</span>                   <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
<span class="n">USBProg</span> <span class="n">JTAG</span> <span class="n">Programmer</span>                 <span class="k">no</span>
<span class="n">Raisonance</span> <span class="n">RLink</span> <span class="n">JTAG</span> <span class="n">Programmer</span>        <span class="k">no</span>
<span class="n">Olimex</span> <span class="n">ARM</span><span class="o">-</span><span class="n">JTAG</span><span class="o">-</span><span class="n">EW</span> <span class="n">Programmer</span>           <span class="k">no</span>
<span class="n">CMSIS</span><span class="o">-</span><span class="n">DAP</span> <span class="n">Compliant</span> <span class="n">Debugger</span>            <span class="n">yes</span> <span class="p">(</span><span class="n">auto</span><span class="p">)</span>
</code></pre></div>


<p>Then we just need to compile everything and install the openocd binary.</p>
<div class="highlight"><pre><span></span><code><span class="n">make</span>

<span class="n">sudo</span> <span class="n">make</span> <span class="n">install</span>
</code></pre></div>


<h2><code>udev</code> rules for USB-Blaster</h2>
<p>After the OpenOCD is installed, we must take care to set the correct <code>udev</code>
rules (access permisions for USB device). As a workaround I have been chmod-ing
the <code>/dev/bus/usb/002/</code> folder to <code>0666</code> and that gave me correct permission
to use the USB-Blaster from Altera Quartus software.</p>
<p>The more elegant solution is described in the comment section of <a href="http://www.fpga-dev.com/altera-usb-blaster-with-
ubuntu/">ALTERA USB-BLASTER
WITH UBUNTU 14.04</a>. The USB-Blaster has multiple personalities (one for FPGA JTAG and
one for ARM JTAG), the udev rule therefore needs to specify both 6010 and
6810 as the targeted devices.</p>
<p>Create /etc/udev/rules.d/51-usbblaster.rules with the following content:</p>
<div class="highlight"><pre><span></span><code><span class="err"># For Altera USB-Blaster on SoCkit</span>
<span class="err">SUBSYSTEM==&quot;usb&quot;,\</span>
<span class="err">ENV{DEVTYPE}==&quot;usb_device&quot;,\</span>
<span class="err">ATTR{idVendor}==&quot;09fb&quot;,\</span>
<span class="err">ATTR{idProduct}==&quot;6010|6810&quot;,\</span>
<span class="err">MODE=&quot;0666&quot;,\</span>
<span class="err">NAME=&quot;bus/usb/$env{BUSNUM}/$env{DEVNUM}&quot;,\</span>
<span class="err">RUN+=&quot;/bin/chmod 0666 %c&quot;</span>
</code></pre></div>


<p><code>udev</code> rules should be reloaded with the following command to take effect
immediately:</p>
<div class="highlight"><pre><span></span><code><span class="err">sudo udevadm control --reload</span>
</code></pre></div>


<h2>First checks</h2>
<p>Now we can try running Altera <code>jtagconfig</code> program to check if the permissions
are OK. When the SoCkit board is attached, the output should look something
like this:</p>
<div class="highlight"><pre><span></span><code><span class="err">➜  ~ ~/altera/16.0/quartus/bin/jtagconfig                                                                                            </span>
<span class="err">1) CV SoCKit [1-1.1]                          </span>
<span class="err">  02D020DD   5CSEBA6(.|ES)/5CSEMA6/..</span>
<span class="err">  4BA00477   SOCVHPS</span>
</code></pre></div>


<p>Now we can also try running OpenOCD:</p>
<div class="highlight"><pre><span></span><code><span class="err">➜  ~ openocd -f interface/altera-usb-blaster2.cfg -f target/altera_fpgasoc.cfg</span>
<span class="err">Open On-Chip Debugger 0.10.0-dev-00324-g12ff09f (2016-06-26-19:19)</span>
<span class="err">Licensed under GNU GPL v2</span>
<span class="err">For bug reports, read</span>
<span class="c">http://openocd.org/doc/doxygen/bugs.html</span>
<span class="err">Warn : Adapter driver &#39;usb_blaster&#39; did not declare which transports it allows; assuming legacy JTAG-only</span>
<span class="err">Info : only one transport option; autoselect &#39;jtag&#39;</span>
<span class="err">adapter speed: 1000 kHz</span>
<span class="err">cycv_dbginit</span>
<span class="err">Info : Altera USB-Blaster II (uninitialized) found</span>
<span class="err">Info : Loading firmware...</span>
<span class="err">Info : Waiting for renumerate...</span>
<span class="err">Info : Waiting for renumerate...</span>
<span class="err">Info : Altera USB-Blaster II found (Firm. rev. = 1.36)</span>
<span class="err">Info : This adapter doesn&#39;t support configurable speed</span>
<span class="err">Info : JTAG tap: fpgasoc.dap tap/device found: 0x4ba00477 (mfg: 0x23b (ARM Ltd.), part: 0xba00, ver: 0x4)</span>
<span class="err">Info : JTAG tap: fpgasoc.fpga.tap tap/device found: 0x02d020dd (mfg: 0x06e (Altera), part: 0x2d02, ver: 0x0)</span>
<span class="err">Info : DAP transaction stalled (WAIT) - slowing down</span>
<span class="err">Info : DAP transaction stalled (WAIT) - slowing down</span>
<span class="err">Info : DAP transaction stalled (WAIT) - slowing down</span>
<span class="err">Info : fpgasoc.cpu.0: hardware has 6 breakpoints, 4 watchpoints</span>
</code></pre></div>


<p>This gives us information that ARM core was recognized, this is therefore the
correct command to use it later with the GDB debugger.</p>
<h2>Eclipse plug-in</h2>
<p>Now we just have to set-up the Eclipse with the OpenOCD plug-in. I have used the
newest version of Eclipse available at the moment, Eclipse Neon.</p>
<p><img alt="Eclipse Neon" src="./images/debugging_cyclone_soc_openocd/eclipse_neon.png" style="width:500px; display: block; margin-left: auto; margin-right: auto;"></p>
<p><a href="http://gnuarmeclipse.github.io/plugins/install/">This page</a> describes how to
install the OpenOCD plug-in for Eclipse, the easiest way is to drag and drop
install icon into a running instance of Eclipse.</p>
<p>Once this is set, we can create a new project. For simpler (bare-metal)
project it would probably make sense to go with "Makefile Project with
Existing Code". For a Linux kernel debugging the plain Project will be enough.</p>
<p><img alt="Creation of new project in Eclipse" src="./images/debugging_cyclone_soc_openocd/eclipse_new_project.png" style="width:800px; display: block; margin-left: auto; margin-right: auto;"></p>
<p><img alt="Creation of new project in Eclipse" src="./images/debugging_cyclone_soc_openocd/eclipse_new_project_name.png" style="width:800px; display: block; margin-left: auto; margin-right: auto;"></p>
<p>Now let's find the original Linux binary. When building kernel with Yocto, the
created file in deploy directory is <code>zimage</code> file. This is a compressed image
with all debugging symbols stripped out. It is optimized to be used in
embedded environment. We need to find the original <code>vmlinux</code> image before the
debugging symbols were stripped out:</p>
<div class="highlight"><pre><span></span><code><span class="err">➜  tmp git:(jethro) find . -name vmlinux                                       </span>
<span class="err">./work/cyclone5-poky-linux-gnueabi/linux-altera/4.3+gitAUTOINC+5938523338-r0/</span>
<span class="err">linux-cyclone5-standard-build/arch/arm/boot/vmlinux</span>
<span class="err">./work/cyclone5-poky-linux-gnueabi/linux-altera/4.3+gitAUTOINC+5938523338-r0/</span>
<span class="err">linux-cyclone5-standard-build/arch/arm/boot/compressed/vmlinux</span>
<span class="err">./work/cyclone5-poky-linux-gnueabi/linux-altera/4.3+gitAUTOINC+5938523338-r0/</span>
<span class="err">linux-cyclone5-standard-build/vmlinux</span>
<span class="err">./work/cyclone5-poky-linux-gnueabi/linux-altera/4.4+gitAUTOINC+969478b841-r0/</span>
<span class="err">linux-cyclone5-standard-build/arch/arm/boot/vmlinux</span>
<span class="err">./work/cyclone5-poky-linux-gnueabi/linux-altera/4.4+gitAUTOINC+969478b841-r0/</span>
<span class="err">linux-cyclone5-standard-build/arch/arm/boot/compressed/vmlinux</span>
<span class="err">./work/cyclone5-poky-linux-gnueabi/linux-altera/4.4+gitAUTOINC+969478b841-r0/</span>
<span class="err">linux-cyclone5-standard-build/vmlinux</span>
</code></pre></div>


<p>There are various images from various different runs (and different versions
of the kernel).</p>
<p>In Eclipse we then select: <code>Run -&gt; Debug Configurations...</code></p>
<p>And then we just need to setup the correct parameters for debug. Under "Main
tab" we need to select the right Linux binary image:</p>
<p><img alt="Creation of new project in Eclipse" src="./images/debugging_cyclone_soc_openocd/eclipse_debug_main.png" style="width:800px; display: block; margin-left: auto; margin-right: auto;"></p>
<p>Under "Debugger tab" we need to set-up the OpenOCD setting and use the correct
gdb (the one produced by Yocto):</p>
<p><img alt="Creation of new project in Eclipse" src="./images/debugging_cyclone_soc_openocd/eclipse_debug_debugger.png" style="width:800px; display: block; margin-left: auto; margin-right: auto;"></p>
<p>If we want to attach to a running kernel, we should un-check "Inital Reset"
and "Load executable" fields under "Startup tab".</p>
<p><img alt="Creation of new project in Eclipse" src="./images/debugging_cyclone_soc_openocd/eclipse_debug_startup.png" style="width:800px; display: block; margin-left: auto; margin-right: auto;"></p>
<p>And we are good to go. After pressing the "Debug" button, the debugging
perspective will show up. Now we can access <code>__log_buf</code> buffer to determine
what is stopping the kernel boot.</p>
<p><img alt="Creation of new project in Eclipse" src="./images/debugging_cyclone_soc_openocd/eclipse_debug_final.png" style="width:800px; display: block; margin-left: auto; margin-right: auto;"></p>
        </div><!-- /.entry-content -->
<a href="https://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="janmarjanovic">Tweet</a><script type="text/javascript" src="https://platform.twitter.com/widgets.js"></script><br/><br/>

      </article>
    </div>
</section>
        <section id="extras" >
        
        </section><!-- /#extras -->
	
        <footer id="contentinfo" >
                <address id="about" class="vcard ">
                Proudly powered by <a href="https://getpelican.com/" target="_blank">Pelican</a>, which takes
                great advantage of <a href="https://python.org" target="_blank">Python</a>.
		
                </address><!-- /#about -->
		

                
        </footer><!-- /#contentinfo -->

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-56555055-1', 'auto');
  ga('send', 'pageview');

</script></body>
</html>